using System;
using System.Collections.Generic;
using System.Text;

using System.Management.Automation;

namespace Microsoft.PowerShell.CmdletManagement
{
    [Cmdlet(VerbsCommon.Remove, "PsSpecCmdletParameter", SupportsShouldProcess = true, DefaultParameterSetName="ById")]
    public class RemoveParameterCommand : PsSpecCommandBase
    {
        // ById Parameter Set
        [Parameter(Mandatory = true, Position = 0, ValueFromPipelineByPropertyName = true, ParameterSetName = "ById")]
        public int Id
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
                idSpecified = true;
            }
        }
        private int _id = -1;
        private bool idSpecified = false;

        // ByName Parameter Set
        [Parameter(Mandatory = false, Position = 0, ValueFromPipelineByPropertyName = true, ParameterSetName="ByName")]
        [ValidateLength(1, 50)]
        public String Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        private string _name = "*";

        [Parameter(Mandatory = true, Position = 1, ValueFromPipelineByPropertyName = true, ParameterSetName = "ByName")]
        public String CmdletName
        {
            get
            {
                return cmdletName;
            }
            set
            {
                cmdletName = value;
            }
        }
        private string cmdletName;

        [ValidateLength(1, 50)]
        [Parameter(Mandatory = true, Position = 2, ValueFromPipelineByPropertyName = true, ParameterSetName="ByName")]
        public string ProjectName
        {
            get { return projectName; }
            set { projectName = value; }
        }
        private string projectName = null;

        protected override void ProcessRecord()
        {
            // Remove multiple cmdlets if specified by name
            if (! idSpecified)
            {
                foreach (PsSpecCmdletParameter parameter in GetParameters(ProjectName, CmdletName, Name))
                {
                    RemoveParameter(parameter.Id);
                }
            }
            else
            {
                RemoveParameter(Id);
            }
        }

        private IEnumerable<PsSpecCmdletParameter> GetParameters(string project, string cmdletName, string name)
        {
            GetPsSpecCmdletParameterCommand command = new GetPsSpecCmdletParameterCommand();
            command.ServiceUri = ServiceUri;
            command.ProjectName = project;
            command.CmdletName = cmdletName;
            command.Name = name;

            foreach (Object result in command.Invoke())
            {
                yield return (PsSpecCmdletParameter)result;
            }
        }

        private void RemoveParameter(int id)
        {
            if (ShouldProcess(
                String.Format("{0}", ServiceUri),
                String.Format(resources.RemovePsSpecCmdletParameterShouldProcess, id)))
            {
                try
                {
                    Service.RemovePsSpecCmdletParameter(id);
                }
                catch (System.Web.Services.Protocols.SoapException e)
                {
                    WriteError(new ErrorRecord(e, "SERVICE_ERROR", ErrorCategory.InvalidOperation, id));
                }
            }
        }
    }
}
